package com.meta.nd.service.impl;


import com.meta.cache.NdCacheService;
import com.meta.common.CommonInfo;
import com.meta.common.ConstDef;
import com.meta.nd.factory.NdFactory;
import com.meta.nd.filter.bo.FilterParamDef;
import com.meta.nd.filter.utils.FilterDefBuilder;
import com.meta.nd.filter.vo.FilterResultVo;
import com.meta.nd.service.NdService;
import com.meta.nd.service.NvFilterService;
import com.meta.nd.service.NvService;
import com.meta.nd.util.NvTool;
import com.meta.nd.vo.Nd;
import com.meta.nd.vo.Nv;
import com.meta.rule.Rule;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class NdServiceImpl  implements NdService {
	
	@Resource
	NvService nvService;

	@Override
	public long saveNd(Nd nd) {

		Nv nv = NvTool.nd2Instance(nd);
		
		return nvService.saveNv(CommonInfo.getDefault(),nv);
		
	}

	@Override
	public Nd getNd(long id) {
		Nv rootNv = this.nvService.getNv(CommonInfo.getDefault(),ConstDef.ND_CODE_ND,id);
		return  NdFactory.createNdByNv(rootNv);
	}

@Override
public Nd getNdByCode(String ndCode){
	Nv rootNv = this.nvService.getNvByCode(CommonInfo.getDefault(),ndCode, ConstDef.FD_NAME_ND_CODE, ConstDef.ND_CODE_ND);
	return  NdFactory.createNdByNv(rootNv);
}

	@Override
	public int deleteNd(long id) {
		// TODO Auto-generated method stub
		return 0;
	}

@Override
public String getRootNdCode(String ndCode){
	Nv nv = nvService.getNvByCode(CommonInfo.getDefault(),ndCode, ConstDef.FD_NAME_ND_CODE, ConstDef.ND_CODE_ND);
	long parentId = nv.getId();
	while( parentId != 0){
		nv = nvService.getSingleNvWithoutFv(CommonInfo.getDefault(),ConstDef.ND_CODE_ND,parentId);
		parentId = nv.getParentId();
	}
	return nv.getRawStringByName(ConstDef.FD_NAME_ND_CODE);
}


	public Rule getRule(String rdCode){
		Nv rootNv = this.nvService.getNvByCode(CommonInfo.getDefault(),rdCode, ConstDef.FD_NAME_CODE, ConstDef.ND_CODE_RULE_ND);
		Rule rule = new Rule();
		rule.setCode(rootNv.getRawStringByName(ConstDef.FD_NAME_CODE));
		rule.setName(rootNv.getRawStringByName(ConstDef.FD_NAME_NAME));
		rule.setDescription(rootNv.getRawStringByName(ConstDef.FD_NAME_DESCRIPTION));
		rule.setOrderType(rootNv.getRawStringByName(ConstDef.FD_NAME_ORDER_TYPE));
		rule.setExecuteOrder(rootNv.getRawIntByName(ConstDef.FD_NAME_EXECUTE_ORDER));
		rule.setTransactionType(rootNv.getRawStringByName(ConstDef.FD_NAME_TRANSACTION_TYPE));
		rule.setRuleText(rootNv.getRawStringByName(ConstDef.FD_NAME_RULE_TEXT));
		return rule;
	}


}
